package com.rainxing.cockpit.modules.hotline.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.rainxing.cockpit.modules.hotline.mapper.HotlineMapper;
import com.rainxing.cockpit.modules.hotline.mapper.InitdepartMapper;
import com.rainxing.cockpit.modules.hotline.pojo.Hotline;
import com.rainxing.cockpit.modules.hotline.service.HotlineService;
import com.rainxing.cockpit.redis.RedisHandle;
import com.rainxing.cockpit.utils.DataTools;
import com.rainxing.cockpit.utils.DepartmentTools;
import com.rainxing.cockpit.utils.TbAndHbTools;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author yabin
 * @since 2018-07-26
 */
@Slf4j
@Service
public class HotlineServiceImpl extends ServiceImpl<HotlineMapper, Hotline> implements HotlineService {

    //注入热线mapper
    @Autowired
    private HotlineMapper hotlineMapper;
    //redis 工具类
    @Autowired
    private RedisHandle redisHandle;
    @Autowired
    private InitdepartMapper initdepartMapper;

    /**
     * 热线电话受理来源、基本执法人员和有效执法人员
     *
     * @param startTime
     * @param endTime
     * @return
     */
    @Override
    public Map<String, Integer> sourceCount(String startTime, String endTime) {
        Map<String, Integer> map = new HashMap<>();
        Long start = System.currentTimeMillis();

        //TODO 这里时间也是定死的
        String startMonth = endTime.substring(0, 7) + "-01 00:00:00";
        //String startMonth="2018-08-01 00:00:00";


        //市局派发的总数
        int count1 = hotlineMapper.selectCount(new EntityWrapper<Hotline>()
                .between("BeginTime", startTime, endTime)
                .andNew()
                .eq("DepamentID", "fc0deb0e-47f3-4cc5-a516-32542487a397"));
        map.put("count1", count1);
        //群众举报的总数
        int counts = hotlineMapper.selectCount(new EntityWrapper<Hotline>()
                .between("BeginTime", startTime, endTime)
                .andNew()
                .eq("EventSource", "电话").or().eq("EventSource", "公众号"));
        map.put("count2", counts);
        //政府热线的总数
        int count3 = hotlineMapper.selectCount(new EntityWrapper<Hotline>()
                .between("BeginTime", startTime, endTime)
                .eq("EventSource", "市长热线"));
        map.put("count3", count3);

        //当月市局派发的总数
        int monthCount1 = hotlineMapper.selectCount(new EntityWrapper<Hotline>()
                .between("BeginTime", startMonth, endTime)
                .andNew()
                .eq("EventSource", "96310").or().eq("EventSource", "市长热线").or().eq("EventSource", "外网"));
        map.put("monthCount1", monthCount1);
        //当月群众举报的总数
        int monthCounts = hotlineMapper.selectCount(new EntityWrapper<Hotline>()
                .between("BeginTime", startMonth, endTime)
                .andNew()
                .eq("EventSource", "电话").or().eq("EventSource", "公众号"));
        map.put("monthCount2", monthCounts);
        //当月政府热线的总数
        int monthCounts3 = hotlineMapper.selectCount(new EntityWrapper<Hotline>()
                .between("BeginTime", startMonth, endTime)
                .eq("EventSource", "市长热线"));
        map.put("monthCounts3", monthCounts3);
        //写入缓存
        redisHandle.set("hr:" + "hotLineSourceCount", map);
        Long end = System.currentTimeMillis();
        log.info("热线电话受理来源:>总共耗时:>" + DataTools.formatDouble1(end - start) * 0.001 + "s");
        return map;
    }

    /**
     * 热线案由Top10
     *
     * @param startTime
     * @param endTime
     * @return
     */
    @Override
    public Map<String, Object> hotLineTop10(String startTime, String endTime) {
        Long start = System.currentTimeMillis();
        Map<String, Object> map = new HashMap<>();
        Map<String, Object> tbMap = new HashMap<>();
        Map<String, Object> hbMap = new HashMap<>();
        List<Map<String, Object>> top10List = null;
        List<Map<String, Object>> tbtop10List = null;
        List<Map<String, Object>> hbtop10List = null;
        //根据案由查询所有热线的总数
        top10List = hotlineMapper.getCaseTop10ByTime(startTime, endTime);

        String upStart = DataTools.upTime(startTime);
        String upEnd = DataTools.upTime(endTime);
        //同比
        tbtop10List = hotlineMapper.getCaseTop10ByTime(upStart, upEnd);
        String newStr = DataTools.subMonth(startTime);
        String endStr = DataTools.subMonth(endTime);
        hbtop10List = hotlineMapper.getCaseTop10ByTime(newStr, endStr);
        map.put("top10List", top10List);
        //计算同比
        tbMap = TbAndHbTools.getAllCount(top10List, tbtop10List);
        hbMap = TbAndHbTools.getAllCount(top10List, hbtop10List);
        map.put("tbMap", tbMap);
        map.put("hbMap", hbMap);
        for (Map<String, Object> m : top10List) {
            String dname = String.valueOf(m.get("CaseOrighName4"));
//            String str=dname.substring(dname.indexOf("、")+1 );
            String str = DepartmentTools.caseAsName(dname);
            m.put("CaseOrighName4", str);
        }
        redisHandle.set("hr:" + "top10Map", map);
        Long end = System.currentTimeMillis();
        log.info("热线案由Top10:>总共耗时:>" + DataTools.formatDouble1(end - start) * 0.001 + "s");
        return map;
    }

    /**
     * 首页 各月份中各执法队举报量排名
     *
     * @param startTime
     * @param endTime
     * @return
     */
    @Override
    public List<Map<String, String>> getReportMonthList(String startTime, String endTime) {
        String key = "hr:";
        List<Map<String, String>> list = hotlineMapper.getReportCountList(startTime, endTime);
        redisHandle.set(key + "reportMonthList", list);
        return list;
    }

    /**
     * 街道下各执法队排名
     *
     * @param startTime
     * @param endTime
     * @return
     */
    @Override
    public Map<String, Object> getReportDepartmentList(String startTime, String endTime) {
        String key = "hr:";
        Map<String, Object> map = new LinkedHashMap<>();
        Map<String, Object> tbMap = new HashMap<>();
        Map<String, Object> hbMap = new HashMap<>();
        List<Map<String, Object>> rankList = hotlineMapper.getReportMonthList(startTime, endTime);
        String upStart = DataTools.upTime(startTime);
        String upEnd = DataTools.upTime(endTime);
        //同比
        List<Map<String, Object>> tbList = hotlineMapper.getReportMonthList(upStart, upEnd);
        String newStr = DataTools.subMonth(startTime);
        String endStr = DataTools.subMonth(endTime);
        List<Map<String, Object>> hbList = hotlineMapper.getReportMonthList(newStr, endStr);
        map.put("rankList", rankList);
        //计算同比
        tbMap = TbAndHbTools.getRankCount(rankList, tbList);
        hbMap = TbAndHbTools.getRankCount(rankList, hbList);
        map.put("tbMap", tbMap);
        map.put("hbMap", hbMap);
        redisHandle.set(key + "reportDepartmentMap", map);
        return map;
    }

    /**
     * 首页 热线举报量排名前三
     *
     * @param startTime
     * @param endTime
     * @return
     */
    @Override
    public Map<String, Object> getReportTop3(String startTime, String endTime) {
        Map<String, Object> map = new HashMap<>();
        Map<String, Object> tbMap = new HashMap<>();
        Map<String, Object> hbMap = new HashMap<>();
        List<Map<String, Object>> reportTop3List = null;
        List<Map<String, Object>> tbreportTop3List = null;
        List<Map<String, Object>> hbreportTop3List = null;
        //根据案由查询所有热线的总数
        reportTop3List = hotlineMapper.getReportTop3(startTime, endTime, 3);
        String upStart = DataTools.upTime(startTime);
        String upEnd = DataTools.upTime(endTime);
        //同比
        int scort = Integer.valueOf(upEnd.substring(5, 7)) - Integer.valueOf(upStart.substring(5, 7));
        tbreportTop3List = hotlineMapper.getReportTop3(upStart, upEnd, scort);
        String newStr = DataTools.subMonth(startTime);
        String endStr = DataTools.subMonth(endTime);
        hbreportTop3List = hotlineMapper.getReportTop3(newStr, endStr, scort);
        map.put("reportTop3List", reportTop3List);
        //计算同比
        tbMap = TbAndHbTools.getMonthsCount(reportTop3List, tbreportTop3List);
        hbMap = TbAndHbTools.getHBMonthsCount(reportTop3List, hbreportTop3List);
        map.put("tbMap", tbMap);
        map.put("hbMap", hbMap);
        redisHandle.set("hr:" + "reportTop3", map);
        return map;
    }

    /**
     * 首页举报量上升、举报量下降
     *
     * @return
     */
    @Override
    public Map<String, Object> getReportRankList() {
        Map<String, Object> map = new HashMap<>();
        Map<String, Object> hbMap = new HashMap<>();
        String key = "hr:";
        //初始化执法队
        List<Map<String, Object>> initList = initdepartMapper.initList();
        List<Map<String, Object>> initEndList = initdepartMapper.initList();

        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        //得到当前时间的上周时间
        String[] split = DataTools.getLastWeekTimeInterval(formatter).split(",");
        String startTime = split[0];
        String endTime = split[1];
        //TODO 此处时间因为数据库不是最新数据，先定死有数据的时间内
        List<Map<String, Object>> list = hotlineMapper.getReportRankList(startTime, endTime);
        List<Map<String, Object>> strList = DepartmentTools.initDepartName(initList, list);
        //得到当前时间的上上周时间
        String[] split2 = DataTools.getLastWeekTimeInterva2(formatter).split(",");
        String upStartTime = split2[0];
        String upEndTime = split2[1];
        List<Map<String, Object>> tblist = hotlineMapper.getReportRankList(upStartTime, upEndTime);
        List<Map<String, Object>> endList = DepartmentTools.initDepartName(initEndList, tblist);
        hbMap = TbAndHbTools.getHBWeekCount(strList, endList);
        map.put("hbMap", hbMap);
        redisHandle.set(key + "reportRank", map);
        return map;
    }

    /**
     * 首页 各个统计数、百分比
     *
     * @param startTime
     * @param endTime
     * @return
     */
    @Override
    public Map<String, Object> getIndexCount(String startTime, String endTime) {
        Map<String, Object> map = new LinkedHashMap<>();
        //热线案件总数
        int hotlineCount = hotlineMapper.selectCount(new EntityWrapper<Hotline>()
                .between("BeginTime", startTime, endTime)
                .notIn("EventSource", "96310")
                .notIn("governTeamName", "怀柔大队指挥中心", "宣教科", "督察一队", "监察科"));
        //热线结案的案件总数
        int overCount = hotlineMapper.selectOverCount(startTime, endTime);
        //办结率
        String banjie = TbAndHbTools.accuracy(overCount, hotlineCount, 2);
        //热线回复的数量
        int a = hotlineMapper.getDealStateCount(startTime, endTime);
        //热线需要回复的总数量
        int b = hotlineMapper.getDealStateAllCount(startTime, endTime);
        //办结率
        String huifu = TbAndHbTools.accuracy(a, b, 2);
        //热线有效举报数
        int real = hotlineMapper.getDealStateReal(startTime, endTime);
        //热线所有
        int allReal = hotlineMapper.getDealStateAllReal(startTime, endTime);
        //办结率
        String youxiao = TbAndHbTools.accuracy(real, allReal, 2);

        map.put("banjie", banjie);
        map.put("huifu", huifu);
        map.put("youxiaojubao", youxiao);
        return map;
    }

    /**
     * 街道下各执法队详情
     *
     * @param startTime
     * @param endTime
     * @return
     */
    @Override
    public Map<String, Object> getReportDepartmentInfo(String startTime, String endTime) {
        String key = "hr:";
        Map<String, Object> map = new LinkedHashMap<>();
        List<Map<String, Object>> rankList = hotlineMapper.getReportMonthInfo(startTime, endTime);
        map.put("rankList", rankList);
        redisHandle.set(key + "reportInfoMap", map);
        return map;
    }
}
